package com.example.demo.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.common.Result;
import com.example.demo.entity.Sojob;
import com.example.demo.entity.Top100InternetCompany;
import com.example.demo.entity.result.MethodRatio;
import com.example.demo.entity.result.NameValue;
import com.example.demo.mapper.SojobMapper;
import com.pig4cloud.plugin.excel.annotation.ResponseExcel;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;

/***
 ** Create by Wedream on 2021/7/16
 */
@RestController
@RequestMapping("/sojob")
public class SojobController {

    @Resource
    SojobMapper sojobMapper;

    @PostMapping  //增
    public Result<?> save(@RequestBody Sojob sojob) {
        sojobMapper.insert(sojob);
        return Result.success();
    }

    @DeleteMapping("/{id}") //删
    public Result<?> delete(@PathVariable long id) {
        sojobMapper.deleteById(id);
        return Result.success();
    }

    @GetMapping //查
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "2000") Integer pageSize,
                              @RequestParam(defaultValue = "",required = false) String search) {

        LambdaQueryWrapper<Sojob> wrapper = Wrappers.<Sojob>lambdaQuery();
        if(StrUtil.isNotBlank(search)) {
           wrapper.like(Sojob::getJobName,search);
        }
        Page<Sojob> sojobPage = sojobMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
        return Result.success(sojobPage);
    }

    @GetMapping("/tags") //统计公司喜欢用的招聘标签
    public Result<?> calTags(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "2000") Integer pageSize,
                              @RequestParam(defaultValue = "",required = false) String search) {

        LambdaQueryWrapper<Sojob> wrapper = Wrappers.<Sojob>lambdaQuery();
        if(StrUtil.isNotBlank(search)) {
            wrapper.like(Sojob::getJobName,search);
        }
        Page<Sojob> sojobPage = sojobMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);

        List<Sojob> records = sojobPage.getRecords();
        Map<String,Integer> mp = new HashMap<>();
        List<NameValue> nv20=new ArrayList<>();

        for (Sojob record : records) {
            String tags = record.getTags();
            String[] tag = tags.split("\\.");
            for (String s : tag) {
                if (s.equals("")) {
                    continue;
                }
                if (!mp.containsKey(s)) {
                    mp.put(s, 1);
                } else {
                    mp.put(s, mp.get(s) + 1);
                }
            }
        }

        for(Map.Entry<String, Integer> entry : mp.entrySet()){
//            System.out.println("foreachEntry : key :" + entry.getKey() + "---> value :"+entry.getValue());
                nv20.add(new NameValue(entry.getKey(),entry.getValue()));
        }

        Collections.sort(nv20);
        Collections.reverse(nv20);

        List<NameValue> res=nv20.subList(0,20); //最喜欢用的前20条
        Page<NameValue> nameValuePage = new Page();  // 创建NameValue类型的Page,前端需要该类型的数据
        nameValuePage.setRecords(res);
        nameValuePage.setTotal(sojobPage.getTotal());
        nameValuePage.setSize(sojobPage.getSize());
        nameValuePage.setCurrent(sojobPage.getCurrent());
        nameValuePage.setOrders(sojobPage.getOrders());
        nameValuePage.setPages(sojobPage.getPages());

        return Result.success(nameValuePage);
    }


    @GetMapping("/city") //统计各城市招聘人数
    public Result<?> calJobs(@RequestParam(defaultValue = "1") Integer pageNum,
                             @RequestParam(defaultValue = "2000") Integer pageSize,
                             @RequestParam(defaultValue = "",required = false) String search) {

        LambdaQueryWrapper<Sojob> wrapper = Wrappers.<Sojob>lambdaQuery();
        if(StrUtil.isNotBlank(search)) {
            wrapper.like(Sojob::getJobName,search);
        }
        Page<Sojob> sojobPage = sojobMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);

        List<Sojob> records = sojobPage.getRecords();
        Map<String,Integer> mp = new HashMap<>();
        List<NameValue> nv20=new ArrayList<>();

        for (Sojob record : records) {
            String city = record.getCity();
            int index=city.indexOf('-');
            if(index!=-1) {
                city=city.substring(0,index);
            }

                if (!mp.containsKey(city)) {
                    mp.put(city, 1);
                } else {
                    mp.put(city, mp.get(city) + 1);
                }
            }


        for(Map.Entry<String, Integer> entry : mp.entrySet()){
//            System.out.println("foreachEntry : key :" + entry.getKey() + "---> value :"+entry.getValue());
            nv20.add(new NameValue(entry.getKey(),entry.getValue()));
        }

        Collections.sort(nv20);
        Collections.reverse(nv20);

        Page<NameValue> nameValuePage = new Page();  // 创建NameValue类型的Page,前端需要该类型的数据
        nameValuePage.setRecords(nv20);
        nameValuePage.setTotal(sojobPage.getTotal());
        nameValuePage.setSize(sojobPage.getSize());
        nameValuePage.setCurrent(sojobPage.getCurrent());
        nameValuePage.setOrders(sojobPage.getOrders());
        nameValuePage.setPages(sojobPage.getPages());

        return Result.success(nameValuePage);
    }

    // 导出Excel
    @ResponseExcel(name = "sojob", sheet = "sojob")
    @GetMapping("/downloadExcel")
    public List<Sojob> DownloadExcel(@RequestParam(defaultValue = "1") Integer pageNum,
                                                     @RequestParam(defaultValue = "1000") Integer pageSize,
                                                     @RequestParam(defaultValue = "",required = false) String search) {

        LambdaQueryWrapper<Sojob> wrapper = Wrappers.<Sojob>lambdaQuery();
        if(StrUtil.isNotBlank(search)) {
            wrapper.like(Sojob::getJobName,search);
        }
        Page<Sojob> sojobPage = sojobMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
        List<Sojob> records = sojobPage.getRecords();
        return records;
    }


    @PutMapping //改
    public Result<?> update(@RequestBody Sojob sojob) {
        sojobMapper.updateById(sojob);
        return Result.success();
    }
}
